package com.lovo.invoicing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.invoicing.bean.*;
import com.lovo.invoicing.bean.vo.OutGoodsBean;
import com.lovo.invoicing.service.*;
import com.lovo.invoicing.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IMailService mailService;

    //入库
    @RequestMapping("/storage")
    public String storage(@RequestBody BuyInfoBean buyInfoBean, HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        goodsService.storage(buyInfoBean,u.getUserId());
        return "success";
    }

    @RequestMapping("/outbound")
    public String outbound(SaleInfoBean saleInfoBean, HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        goodsService.outbound(saleInfoBean,u.getUserId());
        if (saleInfoBean.getSaleTotal()>10000){
            Email email = new Email();
            email.setUser(new String[]{"2372969264@qq.com"});
            email.setSubject("出库");
            String content = "尊敬的杨女士，您成功购物"+saleInfoBean.getSaleTotal()+",感谢惠顾!希望你的可爱，可以治愈一切不可爱";
            email.setContent(content);
            mailService.sendSimpleMail(email);
        }
        return "success";
    }

    @RequestMapping("findGoodsByPage")
    public IPage<GoodsBean> findGoodsByPage(@RequestBody PageBean<GoodsBean> pageBean){
        return goodsService.queryGoodsByPage(pageBean);
    }

    @RequestMapping("/findGoodsById")
    public GoodsBean findGoodsById(int goodsId){
        return goodsService.queryGoodsById(goodsId);
    }

    @RequestMapping("/findGoodsByGoodsNo")
    public GoodsBean findGoodsByGoodsNo(String goodsNo){
        return goodsService.queryGoodsByGoodsNo(goodsNo);
    }

    @RequestMapping("/modifySalePrice")
    public String modifySalePrice(int goodsId, int salePrice){
        goodsService.updateSalePrice(goodsId, salePrice);
        return "success";
    }

    @RequestMapping("/uploadGoodsImg")
    public String uploadGoodsImg(@RequestParam("goodsImg")MultipartFile file) throws IOException {
        return FileUtils.uploadFile(file);
    }

    @RequestMapping("/findOutGoodsByPage")
    public IPage<OutGoodsBean> findOutGoodsByPage(int pageNo,int pageSize,String goodsName,
                                                  int goodsType){
        return goodsService.queryOutGoodsByPage(new Page<>(pageNo,pageSize),goodsName,goodsType);
    }
}
